package com.leyou.orders.service;

import com.leyou.auth.pojo.UserInfo;
import com.leyou.orders.interceptor.UserInterceptor;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.JsonUtils;
import com.leyou.orders.pojo.Cart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author huangxin
 * @date 2019/5/13-12:07
 */
@Service
public class CartService {
    @Autowired
    private StringRedisTemplate redisTemplate;

    private static final String KEY_PREFIX = "cart:user:id:";

    public void addCart(Cart cart) {
        //获取登陆用户
        UserInfo user = UserInterceptor.getUser();
        //key
        String key = KEY_PREFIX + user.getId().toString();
        //hashKey
        String hashKey = cart.getSkuId().toString();
        //记录num
        Integer num = cart.getNum();

        BoundHashOperations<String, Object, Object> operations = redisTemplate.boundHashOps(key);
        //判断当前购物车商品是否存在
        if (operations.hasKey(hashKey)) {
            //存在,修改数量
            String json = (String) operations.get(hashKey);
            cart = JsonUtils.parse(json, Cart.class);
            cart.setNum(cart.getNum()+ num);
        }
        //写回redis
        operations.put(hashKey,JsonUtils.serialize(cart));
    }

    public List<Cart> queryCartList() {
        //获取登陆用户
        UserInfo user = UserInterceptor.getUser();
        //key
        String key = KEY_PREFIX + user.getId().toString();
        //判断购物车是否存在
        if (!redisTemplate.hasKey(key)){
            throw new LyException(ExceptionEnum.CART_NOT_FOUND);
        }

        BoundHashOperations<String, Object, Object> operations = redisTemplate.boundHashOps(key);

        List<Object> values = operations.values();
        List<Cart> carts = values.stream().map(o -> JsonUtils.parse(o.toString(), Cart.class)).collect(Collectors.toList());
        return carts;
    }

    public void updateCartNum(Long skuId, Integer num) {
        //获取登陆用户
        UserInfo user = UserInterceptor.getUser();
        //key
        String key = KEY_PREFIX + user.getId().toString();
        //hashKey
        String hashKey = skuId.toString();
        //判断是否存在
        if (!redisTemplate.hasKey(key)){
            throw new LyException(ExceptionEnum.CART_NOT_FOUND);
        }

        BoundHashOperations<String, Object, Object> operations = redisTemplate.boundHashOps(key);
        //查询购物车
        Cart cart = JsonUtils.parse(operations.get(hashKey).toString(), Cart.class);

        cart.setNum(num);

        //写回redis
        operations.put(hashKey,JsonUtils.serialize(cart));
    }

    public void deleteCart(Long skuId) {
        //获取登陆用户
        UserInfo user = UserInterceptor.getUser();
        //key
        String key = KEY_PREFIX + user.getId().toString();
        //删除
        redisTemplate.opsForHash().delete(key,skuId.toString());
    }

    public void addCarts(List<Cart> carts) {
        for (Cart cart : carts) {
           addCart(cart);
        }
    }

    public void deleteCarts(List<Long> skuIds) {
        //获取登陆用户
        UserInfo user = UserInterceptor.getUser();
        //key
        String key = KEY_PREFIX + user.getId().toString();
        //判断购物车是否存在
        if (!redisTemplate.hasKey(key)){
            throw new LyException(ExceptionEnum.CART_NOT_FOUND);
        }

        BoundHashOperations<String, Object, Object> operations = redisTemplate.boundHashOps(key);

        //删除购物车
        skuIds.stream().forEach(s->operations.delete(s.toString()));
    }
}
